package net.tngou.iask.util;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;

import net.tngou.iask.pojo.Address;
import net.tngou.iask.pojo.POJO;

/**
 * 省份证信息提取
 * @author tngou@tngou.net
 *
 */
public class IDUtil extends POJO {

	
	public static No GetNo(String ID) {
		if(!ValidateTools.IDcard(ID))return null;
		if(ID.length()==15)
		{	
		  String land = ID.substring(0, 6);
		  int gender= Integer.parseInt(ID.substring(14, 15))%2==0?2:1;	
		   LocalDateTime ldt = DateUtil.parse(ID.substring(6, 12));
		   if(ldt!=null){
			   Date birthday = new Date(DateUtil.toDate(ldt).getTime());	
			   if(birthday!=null)
				 return new IDUtil().new No(land,birthday,gender);
		   }
		}else if(ID.length()==18)
		{
			  String land = ID.substring(0, 6);
			  int gender= Integer.parseInt(ID.substring(16, 17))%2==0?2:1;	
			   LocalDateTime ldt = DateUtil.parse(ID.substring(6, 14)+" 000000");
			   if(ldt!=null){
				   Date birthday = new Date(DateUtil.toDate(ldt).getTime());	
				   if(birthday!=null)
					 return new IDUtil().new No(land,birthday,gender);
			   }
		}
		return null;
	}
	
  public static String  Simple(String ID) {
	   String  simple="";
	   if(!ValidateTools.IDcard(ID))return simple;
	   switch (ID.length()) {
			case 15:
				simple=ID.substring(0,12)+"***";
				break;
		   case 18:
			   simple=ID.substring(0,14)+"****";
				break;
			default:
				break;
			}
	 
	  return simple;
   }
  /**
   * 年龄
   * @param birthday
   * @return
   */
  public static int Age(Date birthday)
  {
	  return LocalDate.now().getYear()-birthday.toLocalDate().getYear();
  }

	/**
	 * 省份证解析
	 */
 public	class No{
	 
	  public No(String  land,Date birthday,int gender) {
		this.land=land;this.birthday=birthday;this.gender=gender;
	  }
	  private String  land;//发证地
	  private Date birthday;//出生日期
	  private int gender ;//性别 0（未知）、 1（男） 、2 （女） 

	public String getLand() {
		return land;
	}
	public void setLand(String land) {
		this.land = land;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public int getGender() {
		return gender;
	}
	public void setGender(int gender) {
		this.gender = gender;
	}
	

	}
}
